<?php

namespace app\model;

use think\Model;

class Prize extends Model
{
    // 表名
    protected $name = 'lucky_prize';
    // 主键
    protected $pk = 'id';
    // 自动写入时间戳
    protected $autoWriteTimestamp = 'timestamp';

    /**
     * 获取所有的奖品
     */
    public static function getAllList()
    {
        return self::order('round ASC')
            ->select();
    }

    /**
     * 获取分页奖品列表
     */
    public static function getList($page = 1, $limit = 10, $where = [], $order = ['round' => 'asc'])
    {
        $query = self::where($where);

        // 获取总条数
        $total = $query->count();

        // 获取当前页数据
        $list = $query
            ->order($order)
            ->page($page, $limit)
            ->select();

        // 判断是否有下一页
        $hasMore = ($page * $limit) < $total;

        return [
            'list' => $list,
            'total' => $total,
            'hasMore' => $hasMore
        ];
    }

    /**
     * 根据活动ID和轮次获取奖品
     */
    public static function getByActivityAndRound($activityId, $round)
    {
        return self::where('activity_id', $activityId)
            ->where('round', $round)
            ->select();
    }

    /**
     * 获取活动的最大轮次
     */
    public static function getMaxRound($activityId)
    {
        $maxRound = self::where('activity_id', $activityId)
            ->max('round');
        return $maxRound ?: 0;
    }
}
